//
//  JWRobotDrawable.m
//  KotH
//
//  Created by James Weatherley on 19/08/2008.
//  Copyright 2008 James Weatherley. All rights reserved.
//

#import "../koth.h"
#import "../wolfiphone.h"
#import "../jwglut/jwglut.h"


@implementation JWRobotManager

#pragma mark Protocol
-(id)initWithEntity:(NSObject*)entity
{
	assert(entity == nil);
	self = [super init];
	robots = [[NSMutableArray alloc] init];
	return self;
}

-(void)dealloc
{
	[super dealloc];
	[robots release];
}

-(NSObject*)entity
{
	return robots;
}

-(void)addEntity:(NSObject*)entity
{
	[robots addObject:entity];
}

-(void)removeEntity:(NSObject*)entity
{
	assert([entity isKindOfClass:[JWRobot class]]);
	JWRobot* robot = (JWRobot*)entity;
	
	[robot square].robot = nil;
	[robots removeObject:robot];
}

-(void)removeAllEntities
{
	[robots removeAllObjects];
}

-(void)draw
{
	GLfloat* colour = 0;
	
	for(JWRobot* robot in robots) {
		
		if([robot selected]) {
			colour = selectedColour;
		} else {
			if(robot.square.endOfLevel) {
				colour = robotWinColour;
			} else {
				colour = robotColour;
			}
		}
		pfglColor4f(colour[0], colour[1], colour[2], 1.0f);
		//qglMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, colour);
		
		JWSquare* square = [robot square];
		//glLoadName(ROBOT_SELECTION_OFFSET + [square number]);
		[self drawRobot:square withHeight:[robot height]];
	}
	//glLoadName(0);
}

#pragma mark Implementation
-(void)addRobot:(JWRobot*)robot
{
	[robots addObject:robot];
}

-(void)drawRobot:(JWSquare*)square withHeight:(double)height;
{
	qglPushMatrix();
	{
		qglTranslatef([square x] + 0.5f, height + 0.4f, [square z] + 0.5f);
		glutSolidCube(0.4);
		qglTranslatef(0.0f, 0.35f, 0.0f);
		glutSolidCube(0.3);
		qglTranslatef(0.1f, -0.65f, 0.0f);
		qglPushMatrix();
		{
			qglScalef(1.0f, 2.0f, 1.0f);
			glutSolidCube(0.1);
			qglTranslatef(-0.2f, 0.0f, 0.0f);
			glutSolidCube(0.1);
		}
		qglPopMatrix();
		qglTranslatef(0.15f, 0.3f, 0.0f);
		glutSolidCube(0.1);
		qglTranslatef(-0.5f, 0.0f, 0.0f);
		glutSolidCube(0.1);
	}
	qglPopMatrix();
}

-(void)postDraw
{
}

@end
